package com.sskj.qiangjin.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.CommonBean;
import com.sskj.qiangjin.presenter.RechargePresenter;
import com.sskj.qiangjin.presenter.WithdrawActivityPresenter;
import com.sskj.qiangjin.ui.ArrowAdapter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;

@Route(path = RConfig.APP_RECHARGE)//充值
public class Recharge2Activity extends BaseActivity<RechargePresenter> {

    @BindView(R.id.tvDesc)
    TextView tvDesc;
    @BindView(R.id.tv_ali)
    TextView tv_ali;
    @BindView(R.id.ll_recharge_type)
    LinearLayout ll_recharge_type;
    @BindView(R.id.et_recharge_price)
    EditText etRechargePrice;
    @BindView(R.id.btn_recharge)
    Button btnRecharge;
    @BindView(R.id.spinner)
    Spinner spinner;
    @Autowired
    String moneyType;

    private int recharge_type;
    private float canMin;//最低提现
    private float beiShu;//提现倍数


    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_recharge2;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        ARouter.getInstance().inject(this);
        setTitle("充值");
        if (moneyType.equals("USDT")) {
            ll_recharge_type.setVisibility(View.GONE);
        } else {
            ll_recharge_type.setVisibility(View.VISIBLE);
        }
        mPresenter.getCommonData();
//        mPresenter.getRecharge();

        RxTextView.textChanges(etRechargePrice)
                .debounce(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(CharSequence::toString)
                .subscribe(s -> {
                    if (TextUtils.isEmpty(s)) {
                        s = "0";
                    }
                    if (NumberUtil.isLittleNumber(s) && NumberUtil.isThirdNumber(s)) {
                        etRechargePrice.setText(NumberUtil.keepDown(s, 2, false));
                        return;
                    }
                });

        ClickUtil.click(btnRecharge, () -> {
            if (TextUtils.isEmpty(etRechargePrice.getText().toString().trim())) {
                ToastUtil.showShort("请输入充值数量");
                return;
            }
            if (moneyType.equals("USDT")) {
                if (Float.valueOf(etRechargePrice.getText().toString().trim()) < canMin) {
                    ToastUtil.showShort("充值数量不能小于" + canMin);
                    return;
                }
                if (Float.valueOf(etRechargePrice.getText().toString().trim()) % beiShu != 0) {
                    ToastUtil.showShort("充值数量需为" + beiShu + "的倍数");
                    return;
                }
                mPresenter.getRechargeUrl(etRechargePrice.getText().toString().trim());
            } else {
                if (Float.valueOf(etRechargePrice.getText().toString().trim()) < 1) {
                    ToastUtil.showShort("充值数量不能小于1");
                    return;
                }
                if (Float.valueOf(etRechargePrice.getText().toString().trim()) > 1000000000) {
                    ToastUtil.showShort("充值数量不能大于1000000000");
                    return;
                }
                mPresenter.getCoinRechargeUrl(etRechargePrice.getText().toString().trim(), recharge_type);
            }
        });

        List<String> payTypes = Arrays.asList(getResources().getStringArray(R.array.app_recharges_types));
        ArrowAdapter spinnerTypeAdapter = new ArrowAdapter(this,
                R.layout.app_item_user_type, payTypes);
        spinnerTypeAdapter.setDropDownViewResource(R.layout.app_item_withdraw_type_drop);
        spinner.setAdapter(spinnerTypeAdapter);
        spinner.setSelection(0);
        spinner.setDropDownVerticalOffset(10);
    }

    public void updataUI(CommonBean commonBean) {
        if (commonBean == null) {
            return;
        }
        canMin = Float.valueOf(commonBean.getBuy_info().getRecharge().getQianshu1());
        beiShu = Float.valueOf(commonBean.getBuy_info().getRecharge().getBeishu1());
        if (moneyType.equals("USDT")) {
            tvDesc.setText(commonBean.getBuy_info().getRecharge().getRecharge());
            etRechargePrice.setHint("请输入充值金额，不可小于" + commonBean.getBuy_info().getRecharge().getQianshu1()
                    + ",且为" + commonBean.getBuy_info().getRecharge().getBeishu1() + "的倍数");
        } else {
            tvDesc.setText(commonBean.getBuy_info().getRecharge().getRecharge_jinbi());
            etRechargePrice.setHint("请输入充值金额，不可小于1");
        }
        if (commonBean.getBuy_info().getRecharge().getWx_open().equals("0")) {
            tv_ali.setVisibility(View.VISIBLE);
            spinner.setVisibility(View.GONE);
            recharge_type = 3;
        } else {
            tv_ali.setVisibility(View.GONE);
            spinner.setVisibility(View.VISIBLE);
            RxAdapterView.itemSelections(spinner)
                    .subscribe(pos -> {
                        recharge_type = pos + 2;
                    });
        }
    }

    public void rechargeSuccess(String rechargeUrl) {
        ARouter.getInstance().build(RConfig.APP_RECHATRGE_WEBVIEW).withString(Constans.RECHARGE_URL, rechargeUrl).navigation();
    }
}
